package com.example.androidtest.demo1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.androidtest.R;
import com.example.androidtest.demo.UploadFile;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

public class MyClientDemo2 extends Activity {

    private Button send = null;

    private TextView info = null;
     Socket client;
    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        this.send = findViewById(R.id.button);

        this.info = findViewById(R.id.tv);

        this.send.setOnClickListener(new SendOnClickListener());

    }
    BufferedReader buf;
    ObjectOutputStream oos;
    volatile boolean isStartTask=false;
    private class SendOnClickListener implements View.OnClickListener {

        @Override

        public void onClick(View v) {

            try {

// 读取返回的数据

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        try {
                            client = new Socket("192.168.1.5", 8888);

                            buf = new BufferedReader(new InputStreamReader(

                                    client.getInputStream()));

                             oos = new ObjectOutputStream(client.getOutputStream());
                            startHeartTimer();
                            Thread.sleep(2000);
                            isStartTask=true;
                            UploadFile myFile = SendOnClickListener.this.getUploadFile();

                            oos.writeObject(myFile);//写文件对象
                            Thread.sleep(2000);
                            isStartTask=false;






//                            oos.close();

                        } catch (Exception e) {
                            try {
                                client.close();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }

                    }

                }).start();

//                String result = buf.readLine(); // 接收返回信息
//
//                System.out.println("**************** " + result);
//
//                if ("true".equals(result)) {
//
//                    MyClientDemo.this.info.setText("操作成功！");
//
//                } else {
//
//                    MyClientDemo.this.info.setText("操作失败！");
//
//                }
//
//                buf.close();



            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        private UploadFile getUploadFile() throws Exception { // 包装了传送数据

            UploadFile myFile = new UploadFile();

            myFile.setTitle("DISNEY公园"); // 设置标题

            myFile.setMimeType("image/jpeg"); // 图片的类型

            File file = new File(getFilesDir()

                    .toString() + File.separator + "kotlincn-docs.pdf");

            InputStream input = null;

            try {

                input = new FileInputStream(file); // 从文件中读取

                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                byte data[] = new byte[1024];

                int len = 0;

                while ((len = input.read(data)) != -1) {

                    bos.write(data, 0, len);

                }

                myFile.setContentData(bos.toByteArray());

                myFile.setContentLength(file.length());

                myFile.setExt("pdf");

            } catch (Exception e) {

                throw e;

            } finally {

                input.close();

            }

            return myFile;

        }

    }

    private Timer timer=new Timer();
    private void startHeartTimer() throws IOException {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (!isStartTask){
                    try {
                        oos.writeObject(getUploadHeart());
                        System.out.println("发送心跳");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        },1000,1000);
    }
    private UploadFile getUploadHeart() throws Exception { // 包装了传送数据

        UploadFile myFile = new UploadFile();

        myFile.setTitle("心跳"); // 设置标题

        myFile.setMimeType("image/jpeg"); // 图片的类型



        InputStream input = null;

        try {

//            input = new FileInputStream(file); // 从文件中读取

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            byte data[] = new byte[1024];

            int len = 0;

//            while ((len = input.read(data)) != -1) {
//
//                bos.write(data, 0, len);
//
//            }
            bos.write(1);
            myFile.setContentData(bos.toByteArray());

            myFile.setContentLength(0);

            myFile.setExt("jpeg");

        } catch (Exception e) {

            throw e;

        } finally {

//            input.close();

        }

        return myFile;

    }
}
